<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace App\Admin;
use Core\Db\Db as Db;
/**
 * Pes 上传模块
 * 由于uploadify的session问题
 * 只能将上传功能抽离出来
 * @author LazyPHP
 * @version 1.0
 */
class Upload extends Common {
    
    public function __construct() {
        //对提交域名进行验证
        //当前代码可能还不够安全，目前只能等待更多人使用时
        //协助发现安全的问题。
        $db = $this->db('option');
        $siteUrl = $db->where('option_name = "siteurl"')->find();
        $findUrl = stripos($siteUrl['value'], $_SERVER['SERVER_NAME']);
        if($findUrl === false){
            header('Allow: POST');
            header('HTTP/1.1 405 ');
            header('Content-Type: text/plain');
            exit;
        }
    }

    public function index() {
        //获取上传的类型
        if (empty($_POST['type'])) {
            $data['status'] = 0;
            $data['info'] = '缺失上传类型';
            exit(json_encode($data));
        }
        $typeArray = array('img', 'file');
        $type = trim($_POST['type']);
        if (!in_array($type, $typeArray)) {
            $data['status'] = 0;
            $data['info'] = '错误的上传类型';
            exit(json_encode($data));
        }
        //对应的上传类型操作
        $db = $this->db('option');
        switch ($type) {
            case 'img':
                $upload_img = $db->where("option_name = 'upload_img' ")->find();
                $fileType = json_decode($upload_img['value'], true);
                $fileTypeMes = '请上传图片格式的文件';
                $fileSizeMes = '图片大于5M，请压缩后再上传';
                $fileSavePath = './upload/img';
                break;
            case 'file':
                $upload_file = $db->where("option_name = 'upload_file' ")->find();
                $fileType = json_decode($upload_file['value'], true);
                $fileTypeMes = '请上传以下格式的文件:' . implode(',', $fileType);
                $fileSizeMes = '文件大于5M，请压缩后再上传';
                $fileSavePath = './upload/file';
                break;
        }

        if (empty($_FILES['Filedata']['name'])) {
            $data['status'] = 0;
            $data['info'] = '请选择您要上传的文件';
            exit(json_encode($data));
        }

        $uploadFileType = pathinfo($_FILES['Filedata']['name']);
        //判断上传的文件是否得到许可
        if (!in_array($uploadFileType['extension'], $fileType)) {
            $data['status'] = 0;
            $data['info'] = $fileTypeMes;
            exit(json_encode($data));
        }
        //此处可扩展上传文件大小
        if ($_FILES['Filedata']['size'] > 5555242800) {
            $data['status'] = 0;
            $data['info'] = $fileSizeMes;
            exit(json_encode($data));
        }
        //存储名
        $saveFileName = uniqid() . '.' . $uploadFileType['extension'];
        //存储路径
        $savePath = $fileSavePath . date('/Y-m-d/');
        //获取图片目录是否存在
        if (is_dir($fileSavePath) === false) {
            mkdir($fileSavePath);
        }
        //创建以日期形式存储的图片目录
        if (is_dir($fileSavePath . date('/Y-m-d')) === false) {
            mkdir($fileSavePath . date('/Y-m-d'));
        }

        /*
         * 此处可以再加一个水印扩展
         */

        //存储图片
        move_uploaded_file($_FILES["Filedata"]["tmp_name"], $savePath . $saveFileName);

        $data['status'] = 1;
        $data['info'] = array("上传成功", substr($savePath . $saveFileName, 1, strlen($savePath . $saveFileName)));
        exit(json_encode($data));
    }

}